//logs.js
const util = require('../../utils/util.js')
Page({
  data: {
    textActive: 0,
    logss: [],
    logsTodayList: [],
    sum: [
      {
        title: "今日番茄次数",
        value: "0"
      },
      {
        title: "累计番茄次数",
        value: "0"
      },
      {
        title: "今日专注时长",
        value: "0分钟"
      },
      {
        title: "累计专注时长",
        value: "0分钟"
      }
    ],
    cateArr: ["工作","学习","思考","写作","运动","阅读"] // 选中的任务种类的循环索引
  },
 //每次打开页面
  onShow: function() {
    var loglist = wx.getStorageSync('logss') || [];
    var todayCount = 0; //今日番茄次数
    var allCount = loglist.length; //总计番茄次数
    var todayTime = 0; //今日专注时长
    var allTime = 0; //累计专注时长
    var todayList = []; // 今日数据列表
    //有数据就遍历
    if(loglist.length > 0){
      loglist.map((item) => {
        //统计各种数据
        if(item.date.substr(0,10) == util.formatTime(new Date).substr(0,10)){
          todayCount += 1;
          todayTime += parseInt(item.time);
          todayList.push(item);
        };
        allTime += parseInt(item.time);
      })
      //更新顶部总计数据
      this.setData({
        "sum[0].value": todayCount,
        "sum[1].value": allCount,
        "sum[2].value": todayTime + "分钟",
        "sum[3].value": allTime + "分钟"
      })
    };
   //统计列表：初始值：今日
    this.setData({
      logsTodayList: todayList,
      logss: todayList
      })
  },
  //点击今日或累计
  changeType(e) {
    var loglist = wx.getStorageSync('logss') || [];
    console.log(e.currentTarget.dataset.index)
    var activeindex = e.currentTarget.dataset.index
    //刷新点击选中的样式
    this.setData({
      textActive: activeindex
    })
    if(activeindex == 1){
      this.setData({
        logss: loglist
      })
    } else if(activeindex == 0){
      this.setData({
        logss: this.data.logsTodayList
      })
    }
  }
  
})
